<?php
namespace api\know\model;

use api\know\model\KnowBaseModel;
use api\know\model\QuestionOptionModel;
use api\know\model\QuestionContentModel;

class QuestionModel extends KnowBaseModel
{
    /**
     * 模型名称
     * @var string
     */
    protected $name = 'know_question';

    public static $QueryField = ['id', 'uuid', 'people', 'score', 'grade_code', 'subject_code', 
    'star_code', 'type_code', 'type_alias', 'difficulty_code', 'origin_type_code', 'origin', 'parent_uuid'];

    public static function getQuestion($uuid)
    {
		$row = QuestionModel::getQuestionItem($uuid);
        if (empty($row)) 
        {
            return [];
        }

        $w = ['is_delete' => 0, 'parent_uuid' => $uuid];
        $rows = QuestionModel::where($w)->select()->toArray();
        
        foreach($rows as $v)
        {
            $c = QuestionModel::getQuestionItem($v['id']);
            if (empty($c))
            {
                continue;
            }
            array_push($row['children'], $c);
        }
        return $row;
    }

    public static function getQuestionItem($uuid)
    {
        $where = ['is_delete' => 0, 'uuid' => $uuid ];
        $row = QuestionModel::where($where)->find();
        if (empty($row)) 
        {
            return [];
        }
        $content = QuestionContentModel::where('question_uuid', $row['uuid'])->find();
        if (empty($content)) 
        {
            return [];
        }

        $options = QuestionOptionModel::where('question_uuid', $row['uuid'])->select();
        foreach($options as &$v1)
        {
            $v1['content'] = QuestionModel::htmlDecode($v1['content']);
        }

        $content['content'] = QuestionModel::htmlDecode($content['content']);
        $content['answer'] = QuestionModel::htmlDecode($content['answer']);
        $content['text_analysis'] = QuestionModel::htmlDecode($content['text_analysis']);
        $content['video_analysis'] = QuestionModel::htmlDecode($content['video_analysis']);

        return ['basic' => $row, 'content' => $content, 'children' => [], 'option' => $options ];
    }

    public static function getQuestionComposite($uuid = '')
    {
        $row = QuestionModel::getQuestionCompositeItem($uuid);
       
        $w = ['is_delete' => 0, 'parent_uuid' => $uuid];
        $rows = QuestionModel::where($w)->select()->toArray();

        foreach($rows as $v)
        {
            $item = QuestionModel::getQuestionCompositeItem($v['uuid'])->find();
            if (empty($item))
            {
                continue;
            }
            array_push($row['children'], $item);
        }
        return $row;
    }

    public static function getQuestionCompositeItem($uuid = '')
    {
        $where = ['is_delete' => 0, 'uuid' => $uuid];
        $row = QuestionModel::where($where)->find();
        if (empty($row)) 
        {
            return [];
        }
        $content = QuestionContentModel::where('question_uuid', $row['uuid'])->find();
        if (empty($content)) 
        {
            return [];
        }

        $options = QuestionOptionModel::where('question_uuid', $row['uuid'])->select();
        foreach($options as &$v1)
        {
            $v1['content'] = QuestionModel::htmlDecode($v1['content']);
        }
        
        $content['content'] = QuestionModel::htmlDecode($content['content']);
        $content['answer'] = QuestionModel::htmlDecode($content['answer']);
        $content['text_analysis'] = QuestionModel::htmlDecode($content['text_analysis']);
        $children = [];

        return ['basic' => $row, 'content' => $content, 'children' => $children, 'option' => $options ];
    }

}